package com.ruoyi.sl.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sl.domain.*;
import com.ruoyi.sl.mapper.*;
import com.ruoyi.sl.pojo.SlOddjobDateForm;
import com.ruoyi.sl.pojo.SlRworkDateForm;
import com.ruoyi.sl.service.ISlDbinfoService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.sl.service.ISlOddjobService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 零工报工Service业务层处理
 * 
 * @author admin
 * @date 2024-06-11
 */
@Service
public class SlOddjobServiceImpl implements ISlOddjobService 
{
    @Autowired
    private SlOddjobMapper slOddjobMapper;
    @Autowired
    private SlOddjobOmemberMapper slOddjobOmemberMapper;
    @Autowired
    private SlOddjobAttendanceMapper slOddjobAttendanceMapper;
    @Autowired
    private SlRfworkMapper slRfworkMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ISlDbinfoService slDbinfoService;
    @Autowired
    private SlWagesDayMapper slWagesDayMapper;


    //获报工单ID和参与人获取参与人的所有报工项
    @Override
    public List<SlOddjob> selectSlOddjobListByRfworkIdAndMember(Long rfworkId,String memberUser){
       return  slOddjobMapper.selectSlOddjobListByRfworkIdAndMember(rfworkId,memberUser);
    }


    /**
     * 查询零工报工
     * 
     * @param oddjobId 零工报工主键
     * @return 零工报工
     */
    @Override
    public SlOddjob selectSlOddjobByOddjobId(Long oddjobId)
    {
        return slOddjobMapper.selectSlOddjobByOddjobId(oddjobId);
    }

    /**
     * 查询零工报工列表
     * 
     * @param slOddjob 零工报工
     * @return 零工报工
     */
    @Override
    public List<SlOddjob> selectSlOddjobList(SlOddjob slOddjob)
    {
        return slOddjobMapper.selectSlOddjobList(slOddjob);
    }

    /**
     * 查询零工报工列表
     *
     * @param oddjobDate 日期
     * @return 零工报工集合
     */
    @Override
    public List<SlOddjob> selectSlOddjobUserListByDate(Date oddjobDate){
        return slOddjobMapper.selectSlOddjobUserListByDate(oddjobDate);
    }

    /**
     * 所有已通过审核的零工报工单
     *
     * @return 集合
     */
    @Override
    public List<SlOddjob> selectAllApprovedSlOddjobList(){
        return slOddjobMapper.selectAllApprovedSlOddjobList();
    }

    /**
     * 所有未提交的报工项
     *
     * @return 集合
     */
    public List<SlOddjob> selectSlOddjobListByNoSubmit(SlOddjob slOddjob){
        return slOddjobMapper.selectSlOddjobListByNoSubmit(slOddjob);
    }

    /**
     * 根据报工单ID查询所有报工项
     *
     * @return 集合
     */
    public List<SlOddjob> selectSlOddjobListByRfworkId(Long rfworkId){
        return slOddjobMapper.selectSlOddjobListByRfworkId(rfworkId);
    }

    /**
     * 新增零工报工
     * 
     * @param slOddjob 零工报工
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSlOddjob(SlOddjob slOddjob)
    {
        slOddjob.setCreateTime(DateUtils.getNowDate());


        int rows = slOddjobMapper.insertSlOddjob(slOddjob);
        insertSlOddjobOmember(slOddjob);

        //更新所有操作人的出勤天数信息
//        slOddjobAttendanceMapper.deleteSlOddjobAttendanceByOddjob(slOddjob);
//        addSlOddjobAttendance(slOddjob);

        return rows;
    }

    /**
     * 修改零工报工
     * 
     * @param slOddjob 零工报工
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSlOddjob(SlOddjob slOddjob)
    {
        slOddjob.setUpdateTime(DateUtils.getNowDate());
        //重新提交审批
        if(null != slOddjob.getRfworkId()){
            SlRfwork slRfwork = slRfworkMapper.selectSlRfworkByRfworkId(slOddjob.getRfworkId());
            //1修改人是报工人，重新审批审核
            if(slOddjob.getOddjobUser().equals(getLoginUser().getUsername())){
                //删除所有待办
                slDbinfoService.deleteSlDbinfoByRfworkId(slOddjob.getRfworkId(),"lgbg");
                //删除工资记录
                slWagesDayMapper.deleteSlWagesDayByRfworkId(slOddjob.getRfworkId());
                //给领导创建待办
                createDbInfo(slOddjob,slRfwork);
                //修改报工单审批状态
                slRfwork.setLeaderPass("1");
                slRfwork.setLeaderDate(null);
                slRfwork.setRecheckPass("1");
                slRfwork.setRecheckDate(null);
                slRfwork.setIsStatistics("1");
                slRfworkMapper.updateSlRfworkSetData(slRfwork);
            }
        }

        slOddjobMapper.deleteSlOddjobOmemberByOddjobId(slOddjob.getOddjobId());
        insertSlOddjobOmember(slOddjob);

        return slOddjobMapper.updateSlOddjob(slOddjob);
    }

    /**
     * 修改报工单时间
     *
     * @param slOddjob 报工单
     * @return 结果
     */
    @Override
    public int updateSlOddjobDateTime(SlOddjob slOddjob)
    {
        slOddjob.setUpdateTime(DateUtils.getNowDate());
        return slOddjobMapper.updateSlOddjob(slOddjob);
    }

    /**
     * 批量删除零工报工
     * 
     * @param oddjobIds 需要删除的零工报工主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSlOddjobByOddjobIds(Long[] oddjobIds)
    {
        slOddjobMapper.deleteSlOddjobOmemberByOddjobIds(oddjobIds);

        return slOddjobMapper.deleteSlOddjobByOddjobIds(oddjobIds);
    }

    /**
     * 删除零工报工信息
     * 
     * @param oddjobId 零工报工主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSlOddjobByOddjobId(Long oddjobId)
    {
        slOddjobMapper.deleteSlOddjobOmemberByOddjobId(oddjobId);
        return slOddjobMapper.deleteSlOddjobByOddjobId(oddjobId);
    }


    /**
     * 将报工单的审核标记改为3，已计算工资
     *
     * @param oddjobId 报工单ID
     * @return 结果
     */
    @Override
    public int updateSlOddjobToOver(Long oddjobId){
        return slOddjobMapper.updateSlOddjobToOver(oddjobId);
    }

    /**
     * 新增零工操作人信息
     * 
     * @param slOddjob 零工报工对象
     */
    public void insertSlOddjobOmember(SlOddjob slOddjob)
    {
        List<SlOddjobOmember> slOddjobOmemberList = slOddjob.getSlOddjobOmemberList();
        Long oddjobId = slOddjob.getOddjobId();
        if (StringUtils.isNotNull(slOddjobOmemberList))
        {
            List<SlOddjobOmember> list = new ArrayList<SlOddjobOmember>();
            for (SlOddjobOmember slOddjobOmember : slOddjobOmemberList)
            {
                slOddjobOmember.setOddjobId(oddjobId);
                slOddjobOmember.setOddjobDate(slOddjob.getOddjobDate());
                slOddjobOmember.setOddjobUser(slOddjob.getOddjobUser());
//                slOddjobOmember.setRfworkId(slOddjob.getRfworkId());
                list.add(slOddjobOmember);
            }
            if (list.size() > 0)
            {
                slOddjobMapper.batchSlOddjobOmember(list);
            }
        }
    }

    /**
     * 更新操作人出勤天数信息
     *
     * @param slOddjob 报工单对象
     */
    public void addSlOddjobAttendance(SlOddjob slOddjob)
    {
        //获取当前报工人和日期下的所有操作人
        List<SlOddjobOmember> slOddjobOmemberList = slOddjobOmemberMapper.selectSlOddjobOmemberListByOddjob(slOddjob);
        //去重
        Set<String> uniqueValues = new HashSet<>();
        List<SlOddjobOmember> distinctList = new ArrayList<>();
        for (SlOddjobOmember om : slOddjobOmemberList) {
            if (uniqueValues.add(om.getOddjobUser())) {
                distinctList.add(om);
            }
        }
        //添加考勤天数
        for (SlOddjobOmember ra : distinctList) {
            SlOddjobAttendance sl = new SlOddjobAttendance();
            sl.setOddjobUser(slOddjob.getOddjobUser());
            sl.setOddjobDate(slOddjob.getOddjobDate());
            sl.setUserName(ra.getOmemberUser());
            sl.setNickName(ra.getOmemberNick());
            sl.setOddjobYear(slOddjob.getOddjobYear());
            sl.setOddjobMonth(slOddjob.getOddjobMonth());
            sl.setOddjobDay(slOddjob.getOddjobDay());
            slOddjobAttendanceMapper.insertSlOddjobAttendance(sl);
        }
    }

    /**
     * 创建一个报工单关联所有报工项
     *
     * @param slOddjobDateForm 报工单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDateOddjobs(SlOddjobDateForm slOddjobDateForm){

        List<SlOddjob> oddjoblist = slOddjobDateForm.getOddjobs();
        SlOddjob slOddjob = oddjoblist.get(0);
        SysUser user = userMapper.selectUserByUserName(slOddjob.getOddjobUser());
        SysUser userLeader = userMapper.selectUserByUserName(slOddjob.getLeaderUser());
        //创建工作单
        SlRfwork slRfwork = new SlRfwork();
        slRfwork.setRfworkDate(slOddjob.getOddjobDate());
        slRfwork.setRfworkUser(slOddjob.getOddjobUser());
        slRfwork.setRfworkNick(user.getNickName());
        slRfwork.setLeaderUser(slOddjob.getLeaderUser());
        slRfwork.setLeaderNick(userLeader.getNickName());
        slRfwork.setModuleId(slOddjob.getModuleId());
        slRfwork.setIsNew("0");
        slRfwork.setLeaderPass("1");
        //slRfwork.setRecheckUser()); //复核人
        //slRfwork.setRecheckNick();    //复核人昵称
        slRfwork.setRecheckPass("1");
        slRfwork.setRfworkYear(slOddjob.getOddjobYear());
        slRfwork.setRfworkMonth(slOddjob.getOddjobMonth());
        slRfwork.setRfworkDay(slOddjob.getOddjobDay());
        slRfwork.setRfwordType("1");    //0计件报工1零工
        slRfworkMapper.insertSlRfwork(slRfwork);
        //更新工作项
        for (SlOddjob oddjob: oddjoblist) {
            oddjob.setRfworkId(slRfwork.getRfworkId());
            slOddjobMapper.updateSlOddjob(oddjob);
        }
        //创建出勤天数
        List<SlOddjobAttendance> slAttendanceList = slOddjobDateForm.getAttendances();
        for (SlOddjobAttendance attendance:slAttendanceList) {
            attendance.setRfworkId(slRfwork.getRfworkId());
            slOddjobAttendanceMapper.insertSlOddjobAttendance(attendance);
        }

        //给领导创建待办
        createDbInfo(slOddjob,slRfwork);

        return 1;
    }

    public void createDbInfo(SlOddjob slOddjob,SlRfwork slRfwork){
        //给领导创建待办
        SysUser sysUser = userMapper.selectUserByUserName(slOddjob.getOddjobUser());
        SysUser leaderUser = userMapper.selectUserByUserName(slOddjob.getLeaderUser());
        SlDbinfo slDbinfo = new SlDbinfo();
        slDbinfo.setDbLevel(String.valueOf(2));
        slDbinfo.setDbSubject(DateUtils.dateTime(slOddjob.getOddjobDate())+"["+sysUser.getNickName()+"]的零工报工单");
        slDbinfo.setIsDb("0");  //待办
        slDbinfo.setReceptionTime(DateUtils.getNowDate()); //接收时间
        slDbinfo.setPresenterNick(sysUser.getNickName());    //提交人名称
        slDbinfo.setPresenterUser(sysUser.getUserName());    //提交人ID
        slDbinfo.setTransactorsNick(leaderUser.getNickName());  //处理人名
        slDbinfo.setTransactorsUser(leaderUser.getUserName());  //处理人ID
        slDbinfo.setIsDone("3");   //状态
        slDbinfo.setSourceId(slRfwork.getRfworkId());
        slDbinfo.setSource("lgbg");   //计件报工
        slDbinfo.setRwUser(slOddjob.getOddjobUser());
        slDbinfo.setRwDate(slOddjob.getOddjobDate());
        slDbinfoService.insertSlDbinfo(slDbinfo);
    }


}
